//
//  CutOutImageController.m
//  QRMaster
//
//  Created by myApple on 16/5/30.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "CutOutImageController.h"
#import "AddQuickCardController.h"

@interface CutOutImageController ()<UIGestureRecognizerDelegate>
{
    UIImageView * submitImageView;
    UILabel * submitLabel;
    UIImageView * imageView;
    float lastScale;
    CGRect minImgRect;
}

@end

@implementation CutOutImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0x000000);
    [self prepareNavView];
    [self prepareView];
    [self preparGestureRecognizer];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    submitImageView.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    submitImageView.hidden = YES;
}

#pragma mark - prepareView
-(void)prepareNavView
{
    submitImageView = [[UIImageView alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 66, 2, 50, 40)];
    submitImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * submitTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(savePicture)];
    [submitImageView addGestureRecognizer:submitTap];
    [self.navigationController.navigationBar addSubview:submitImageView];
    
    submitLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 50, 20)];
    submitLabel.text = NSLocalizedString(@"authenSubmit",@"完成");
    submitLabel.textColor = UIColorFromRGBAndA(0x7dc234, 1);
    submitLabel.font = [UIFont systemFontOfSize:15.0];
    submitLabel.textAlignment = NSTextAlignmentRight;
    [submitImageView addSubview:submitLabel];
}

-(void)prepareView
{
    float x = 0;
    float width = UI_SCREEN_WIDTH;
    float height = 185 * UI_SCREEN_WIDTH /375;
    float y = (UI_SCREEN_HEIGHT - height)/2.0;
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    imageView.userInteractionEnabled = YES;
    minImgRect = imageView.frame;
    imageView.image = _selectImage;
    [self.view addSubview:imageView];
    imageView.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - 64);
    
    //上面黑色的view
    y = 0;
    x = 0;
    height = minImgRect.origin.y;
    width = UI_SCREEN_WIDTH;
    UIView * upView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    upView.backgroundColor = UIColorFromRGBAndA(0x000000, 0.2);
    upView.userInteractionEnabled = NO;
    [self.view addSubview:upView];
    
    //下面的view
    y = minImgRect.origin.y + minImgRect.size.height;
    x = 0;
    height = UI_SCREEN_HEIGHT - y;
    width = UI_SCREEN_WIDTH;
    UIView * downView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    downView.backgroundColor = UIColorFromRGBAndA(0x000000, 0.2);
    downView.userInteractionEnabled = NO;
    [self.view addSubview:downView];
    
}

-(void)preparGestureRecognizer
{
    //捏合缩放
    UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scale:)];
    [pinchRecognizer setDelegate:self];
    [imageView addGestureRecognizer:pinchRecognizer];
    //移动
    UIPanGestureRecognizer * panRecognizer =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(move:)];
    [panRecognizer setDelegate:self];
    [imageView addGestureRecognizer:panRecognizer];
}

//缩放
-(void)scale:(UIPinchGestureRecognizer *)pinch
{
    if ([pinch state] == UIGestureRecognizerStateEnded) {
        lastScale = 1.0;
        //缩放的大小控制
        if (imageView.frame.size.width < UI_SCREEN_WIDTH || imageView.frame.size.height < 185 * UI_SCREEN_WIDTH /375) {
            imageView.frame = minImgRect;
        }
        
    return;
    }
    CGFloat scale = 1.0 - (lastScale - [pinch scale]);
    CGAffineTransform currentTransform = imageView.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [imageView setTransform:newTransform];
    lastScale = [pinch scale];
    
}

//移动
-(void)move:(UIPanGestureRecognizer *)pan
{
    CGPoint translation = [pan translationInView:self.view];
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    if ([pan state] == UIGestureRecognizerStateEnded) {
        //边界控制 不能离开中间的圆框
        if (pan.view.frame.origin.x > 0) {
            pan.view.frame = CGRectMake(0, pan.view.frame.origin.y,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        if (pan.view.frame.origin.y > (UI_SCREEN_HEIGHT - minImgRect.size.height - 64)/2.0) {
            pan.view.frame = CGRectMake(pan.view.frame.origin.x, (UI_SCREEN_HEIGHT - minImgRect.size.height - 64)/2.0,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        if (pan.view.frame.origin.x + pan.view.frame.size.width < ScreenWidth) {
            pan.view.frame = CGRectMake(ScreenWidth - pan.view.frame.size.width, pan.view.frame.origin.y,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        if (pan.view.frame.origin.y + pan.view.frame.size.height <  ScreenHeight / 2.0 + 80) {
            pan.view.frame = CGRectMake(pan.view.frame.origin.x, ScreenHeight / 2.0 + 80 - pan.view.frame.size.height,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        return;
        
    }
    pan.view.center = CGPointMake(pan.view.center.x + translation.x,
                                  pan.view.center.y + translation.y);
    
    [pan setTranslation:CGPointZero inView:self.view];
}

//保存图片（剪切图片）
-(void)savePicture
{
    UIGraphicsBeginImageContext(imageView.frame.size);
    [_selectImage drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
//    UIImage * clipImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([newImage CGImage],CGRectMake(imageView.frame)];
    
     UIImage * clipImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([newImage CGImage],CGRectMake(minImgRect.origin.x - imageView.frame.origin.x,minImgRect.origin.y - imageView.frame.origin.y, minImgRect.size.width, minImgRect.size.height))];
    
    if (_imgBlock) {
        _imgBlock(clipImage);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
